package genetic.q3;

import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import genetic.Genotype;
import genetic.Phenotype;

public class RobbyPhenotype extends Phenotype  {

	private int _games;
	private board _b = new board();
	public RobbyPhenotype(){

	}

	public RobbyPhenotype(int games){
		_games=games;
	}


	@Override
	public Genotype createRandomInstance() {
		Genotype ins = new RobbyGenotype(_games, _b);
		return ins;
	}

	@Override
	public Genotype[] crossover(Genotype g1, Genotype g2) {
		RobbyGenotype g3 = (RobbyGenotype) g1.clone();
		RobbyGenotype g4 = (RobbyGenotype) g2.clone();
		Random r = new Random();

			int spot = r.nextInt(g4.instance.length);
			for(int i=0; i<spot; i++){
				int tmp = g4.instance[i];
				g4.instance[i] = g3.instance[i];
				g3.instance[i] = tmp;
			}
		Genotype[] ans = {g3, g4};
		return ans;
	}

	@Override
	public double fitness(Genotype g) {
		return ((RobbyGenotype)g).getFitness();
	}

	@Override
	public List<Genotype> getInitialPopulation(int populationSize) {
		return getInitialRandomPopulation(populationSize);
	}

	@Override
	public List<Genotype> getMatingPool(List<Genotype> population,
			int populationSize) {

		List<Genotype> newpopulation = new LinkedList<Genotype>();
		for(int i=0; i<populationSize; i++){
			Random r = new Random();
			int first = r.nextInt(populationSize);
			int second = r.nextInt(populationSize);
			Genotype f = population.get(first);
			Genotype s = population.get(second);
			if(fitness(f) > fitness(s))
				newpopulation.add(f);
			else newpopulation.add(s);
		}

		return newpopulation;

	}


	@Override
	public Genotype mutate(Genotype g, double Pm) {
		RobbyGenotype ans = (RobbyGenotype) g.clone();
		ans.instance = ((RobbyGenotype)g).instance;
			bitwiseMutation(ans.instance, Pm, 7);
		return ans;
	}

	@Override
	public void updateFitness(List<Genotype> population) {
		// TODO Auto-generated method stub

	}
}
